package com.ayf.payment.game.api.filter;

import com.alibaba.fastjson.JSON;
import com.ayf.payment.game.api.bean.ErrorBean;
import com.ayf.payment.game.api.exception.AuditedFailedException;
import com.ayf.payment.game.api.exception.AuthorizeFailedException;
import com.ayf.payment.game.api.exception.BusinessException;
import com.ayf.payment.game.api.type.CommonConstants;
import com.swwx.charm.commons.lang.utils.LogPortal;
import com.swwx.charm.commons.lang.utils.SysContent;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

public class BasicFilter {

    //下面这些请求什么都不需要校验
    protected static final List<String> canAlwaysVisits = Arrays.asList(
            "/v/gameGift/receive",
            "/v/gameGift/partitionList",
            "/v/gameGift/list/",
            "/v/gameGift/collectRecord/",
            "/v/merchant/getUrl",
            "/v/game/player/addGameOrder",
            "/v/file/download",
            "/v/good/selectConfig",
            "/v/good/partitionList",
            "/v/proxy/register",
            "/v/box/add",
            "/v/partition/wxSelectAll",
            "/v/reissueRecord/wxSelectAll",
            "/v/reissueRecord/wxManual",
            "/v/reissueRecord/wxSaveAuto",
            "/v/partition/getAllPartition",
            "/v/order/add",
            "/v/order/details",
            "/v/order/getPayQrImage",
            "/v/order/selectStatus",
            "/v/order/playerQueryOrderNo",
            "/v/template/selectParAndTemInfo",
            "/v/merchant/login",
            "/v/merchant/register",
            "/v/merchant/resetPassword",
            "/v/merchant/dowPayUrls",
            "/v/merchant/selectPayProduct",
            "/v/merchant/msgStatus",
            "/v/merchant/getLoginQrCode",
            "/v/merchant/qrCodeLogin",
            "/v/merchant/bindWx",
            "/v/merchant/getRechargeSkin",
            "/v/pay/payCallBack",
            "/v/yhb/pay/notify",
            "/v/site/statistics/flow",
            "/v/jb/pay/notify",
            "/v/jb/pay/returns",
            "/v/yk/pay/notify",
            "/v/yk/pay/returns",
            "/v/yk/pay/agentpay/notify",
            "/v/longbao/pay/notify",
            "/v/longbao/pay/returns",
            "/v/zfy/pay/returns",
            "/v/zfy/pay/notify",
            "/v/wanx/pay/notify",
            "/v/wanx/pay/returns",
            "/v/wanx/pay/paynotify",
            "/v/site/statistics/players",
            "/v/merchant/netConfig",
            "/v/merchant/getMerchantNotice",
            "/v/long/pay/paynotify",
            "/v/box/addWx",
            "/v/qiyi/pay/notify",
            "/v/qiyi/pay/returns",
            "/v/zftong/pay/notify",
            "/v/long/pay/paynotify",
            "/v/qc/pay/notify",
            "/v/qc/pay/returns",
            "/v/long/pay/paynotify",
            "/v/cyf/pay/notify",
            "/v/qiyi/pay/paynotify",
            "/v/wanw/pay/notify",
            "/v/wanw/pay/returns",
            "/v/wanw/pay/paynotify",
            "/v/long/pay/paynotify",
            "/v/xingyou2/pay/notify",
            "/v/xingyou/pay/notify",
            "/v/xingyou/pay/paynotify",
            "/v/gameGift/downloadImg",
            "/v/xingyou/pay/paynotify",
            "/v/merchant/ayb/getAybLoginQrCode",
            "/v/merchant/ayb/getAybLoginStatus",
            "/v/merchant/ayb/getQrCode/",
            "/v/xingyou/pay/paynotify",
            "/v/gameGift/downloadImg",
            "/v/xingyou/pay/paynotify",
            "/v/huandong/pay/notify",
            "/v/huandong/pay/paynotify",
            "/v/qcjh/pay/notify",
            "/v/qcjh/pay/paynotify",
            "/v/huyu/pay/notify",
            "/v/huyu/pay/returns",
            "/v/huyu/pay/paynotify",
            "/v/ayb/pay/notify",
            "/v/ayb/pay/returns",
            "/v/order/getPayParam",
            "/v/ext/",
            "/v/merchant/downloadImg",
            "/v/fzt/pay/notify",
            "/v/fzt/pay/returns",
            "/v/wanxyft/pay/notify",
            "/v/zfyfz/pay/returns",
            "/v/zfyfz/pay/notify",
            "/v/faka/pay/notify",
            "/v/faka/pay/paynotify",
            "/v/dy/pay/notify",
            "/v/sky98/pay/notify",
            "/v/sky98/pay/paynotify"
    );

    //下面这些请求待审核也能访问
    protected static final List<String> notVisits = Arrays.asList(
            "/v/merchant/getSignStatus",
            "/v/merchant/getSignQrCode",
            "/v/merchant/selectOne",
            "/v/merchant/logout",
            "/v/merchant/selectConfig",
            "/v/merchant/updatePassword",
            "/v/merchant/updateQQ",
            "/v/merchant/update",
            "/v/merchant/history",
            "/v/merchant/getMerchantMenu",
            "/v/merchant/getQrCode",
            "/v/merchant/getUnBindQrCode",
            "/v/merchant/getUnBindStatus",
            "/v/merchant/getWxList",
            "/v/merchant/getLoginQrCode",
            "/v/merchant/bindWx",
            "/v/bank/selectOne",
            "/v/bank/insert",
            "/v/bank/list",
            "/v/merchant/ayb/getAybUser",
            "/v/merchant/ayb/getUnBindQrCodeUrl",
            "/v/merchant/uploadImg"
    );

	public boolean isContain(String uri, List<String> list) {
		boolean result = false;
		for (int i = 0; i < list.size(); i++) {
			if (uri.contains(list.get(i))) {
				result = true;
				break;
			}
		}

		return result;
	}

	public void handleException(HttpServletResponse res, Exception e) throws IOException {
		LogPortal.error("异常信息是:", e);
		ErrorBean error = null;
		if (e instanceof AuthorizeFailedException) { //授权失败
			error = new ErrorBean(CommonConstants.RETURN_ERROR, ((AuthorizeFailedException) e).getMsg());
			res.setStatus(HttpStatus.UNAUTHORIZED.value());
		}
		else if (e instanceof BusinessException) { //业务失败
			error = new ErrorBean(CommonConstants.RETURN_ERROR, ((BusinessException) e).getMsg());
			res.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
		}
		else if (e instanceof AuditedFailedException) { //
			error = new ErrorBean(-202, ((AuditedFailedException) e).getMsg());
			res.setStatus(HttpStatus.OK.value());
		}
		else {
			error = new ErrorBean(CommonConstants.RETURN_ERROR, "Server error.");
			res.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
		}

		res.addHeader("clueNo", SysContent.getClueNo());
		res.setHeader("Content-Type", "application/json");
		PrintWriter pw = res.getWriter();
		pw.print(JSON.toJSONString(error));
		pw.close();
	}

}
